package com.yyang.pai.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import com.yyang.pai.di.component.DaggerHomeComponent;
import com.yyang.pai.mvp.contract.HomeContract;
import com.yyang.pai.mvp.presenter.HomePresenter;

import com.yyang.pai.R;
import com.yyang.pai.mvp.ui.adapter.HomePageAdapter;
import com.yyang.pai.mvp.ui.fragment.ContactFragment;
import com.yyang.pai.mvp.ui.fragment.HomesFragment;
import com.yyang.pai.mvp.ui.fragment.TrendsFragment;
import com.yyang.pai.mvp.ui.fragment.WatchFragment;
import com.yyang.pai.utils.StatusBarUtils;


import java.util.ArrayList;

import javax.inject.Inject;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 03/16/2019 21:30
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class HomeActivity extends BaseActivity<HomePresenter> implements HomeContract.View {

    @BindView(R.id.m_viewPage)
    ViewPager mViewPager;
    @BindView(R.id.rg_tab_bar)
    RadioGroup mRadioGroup;
    @BindView(R.id.rb_channel)
    RadioButton rb_channel;
    @BindView(R.id.rb_message)
    RadioButton rb_message;
    @BindView(R.id.rb_better)
    RadioButton rb_better;
    @BindView(R.id.rb_setting)
    RadioButton rb_setting;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerHomeComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        //当FitsSystemWindows设置 true 时，会在屏幕最上方预留出状态栏高度的 padding
        StatusBarUtils.setRootViewFitsSystemWindows(this,true);
        //设置状态栏透明
        StatusBarUtils.setTranslucentStatus(this);
        //一般的手机的状态栏文字和图标都是白色的, 可如果你的应用也是纯白色的, 或导致状态栏文字看不清
        //所以如果你是这种情况,请使用以下代码, 设置状态使用深色文字图标风格, 否则你可以选择性注释掉这个if内容
        if (!StatusBarUtils.setStatusBarDarkTheme(this, true)) {
            //如果不支持设置深色风格 为了兼容总不能让状态栏白白的看不清, 于是设置一个状态栏颜色为半透明,
            //这样半透明+白=灰, 状态栏的文字能看得清
            StatusBarUtils.setStatusBarColor(this,0x55000000);
        }
        //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
        return R.layout.activity_home;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb_channel:
                        mViewPager.setCurrentItem(0);
                        break;
                    case R.id.rb_message:
                        mViewPager.setCurrentItem(1);
                        break;
                    case R.id.rb_better:
                        mViewPager.setCurrentItem(2);
                        break;
                    case R.id.rb_setting:
                        mViewPager.setCurrentItem(3);
                        break;
                    default:;
                }
            }
        });

        FragmentManager supportFragmentManager = getSupportFragmentManager();
        HomesFragment homesFragment = new HomesFragment();
        WatchFragment watchFragment = new WatchFragment();
        TrendsFragment trendsFragment = new TrendsFragment();
        ContactFragment contactFragment = new ContactFragment();
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(homesFragment);
        fragments.add(trendsFragment);
        fragments.add(watchFragment);
        fragments.add(contactFragment);

        mViewPager.setAdapter(new HomePageAdapter(supportFragmentManager, fragments));
        mViewPager.setCurrentItem(0);
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {

            }

            @Override
            public void onPageScrollStateChanged(int i) {
                if (i == 2){
                    switch (mViewPager.getCurrentItem()){
                        case 0:
                            rb_channel.setChecked(true);
                            break;
                        case 1:
                            rb_message.setChecked(true);
                            break;
                        case 2:
                            rb_better.setChecked(true);
                            break;
                        case 3:
                            rb_setting.setChecked(true);
                            break;
                            default:;
                    }
                }
            }
        });
        rb_channel.setChecked(true);

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }
}
